package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Invoice;
import cn.itcast.domain.cargo.InvoiceExample;
import cn.itcast.domain.shipping.Shipping;
import cn.itcast.service.cargo.InvoiceService;
import cn.itcast.service.cargo.ShippingService;
import cn.itcast.web.controller.BaseController;
import cn.itcast.web.utils.DownloadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("cargo/invoice")
public class InvoiceController extends BaseController {
    @Reference(check = false,retries = 0)
    private InvoiceService invoiceService;
    @Reference(check = false,retries = 0)
    private ShippingService shippingService;

    //查看发票列表
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize){

        InvoiceExample invoiceExample = new InvoiceExample();
        invoiceExample.createCriteria().andCompanyIdEqualTo(getLoginCompanyId());

        //1. 查找到所有的发票
        PageInfo<Invoice> pageInfo = invoiceService.findByPage(invoiceExample,pageNum,pageSize);
        //2. 把查询的委托单存储到request域中
        request.setAttribute("pageInfo",pageInfo);
        return "cargo/invoice/invoice-list";
    }

    //保存添加
    @RequestMapping("/edit")
    public String edit(Invoice invoice,String shippingOrderId) {

        //更新委托单的状态
        Shipping shipping = shippingService.findById(shippingOrderId);
        shipping.setState(1);
        shippingService.update(shipping);

        invoice.setInvoiceId(shippingOrderId);
        invoice.setCompanyId(getLoginCompanyId());
        invoice.setCreateby(getLoginUser().getUserName());
        invoice.setDeptId(getLoginUser().getDeptId());
        invoice.setCreatedate(new Date());
        invoice.setStatus("0");
        invoiceService.save(invoice);

        return "redirect:/cargo/invoice/list.do";
    }

    //删除
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(String id){
        //1.根据id删除
        Integer row = invoiceService.delete(id);
        if (row==0){
            return "0";
        }else {
            return "1";
        }
    }

    @RequestMapping("/printExcel")
    @ResponseBody
    public void printExcel(String id) throws IOException {
        InputStream inputStream = session.getServletContext().getResourceAsStream("/make/xlsprint/tOUTINVOICE.xlsx");
        //2. 使用模板输入流创建一个工作薄
        Workbook workbook = new XSSFWorkbook(inputStream);
        //3. 得到工作单
        Sheet sheet = workbook.getSheetAt(0);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        //查询所有的数据，然后遍历设置到excel中
        Invoice invoice = invoiceService.findById(id);

        //得到第11行5格:发票id
        String invoiceId = invoice.getInvoiceId();
        if (invoiceId!=null){
            Row row = sheet.getRow(11);
            Cell cell = row.getCell(5);
            cell.setCellValue(invoiceId);
        }

        //得到第11行5格:发票id
        Date invoiceTime = invoice.getInvoiceTime();
        if (invoiceTime!=null){
            Row row = sheet.getRow(11);
            Cell cell = row.getCell(8);
            cell.setCellValue(dateFormat.format(invoiceTime));
        }

        //得到第13行5格:lcNo
        Shipping shipping = shippingService.findById(id);
        String lcNo = shipping.getLcNo();
        if (lcNo!=null){
            Row row = sheet.getRow(13);
            Cell cell = row.getCell(5);
            cell.setCellValue(lcNo);
        }

        //得到第11行10格:scNo
        String scNo = invoice.getScNo();
        if (scNo!=null){
            Row row = sheet.getRow(11);
            Cell cell = row.getCell(10);
            cell.setCellValue(scNo);
        }

        //得到第13行9格:Port of Loading
        String portOfLoading = shipping.getPortOfLoading();
        if (portOfLoading!=null){
            Row row = sheet.getRow(13);
            Cell cell = row.getCell(9);
            cell.setCellValue(portOfLoading);
        }

        //得到第13行9格:Port of Loading
        String portOfDischar = shipping.getPortOfDischar();
        if (portOfDischar!=null){
            Row row = sheet.getRow(13);
            Cell cell = row.getCell(9);
            cell.setCellValue(portOfDischar);
        }

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    //把workbook的内容写出到字节数组汇总
        workbook.write(byteArrayOutputStream);
        new DownloadUtil().download(byteArrayOutputStream,response,"发票.xlsx");
    }
}
